package com.tuzhi.base.jpa.healper.wenhao;

import com.tuzhi.base.function.SFunction;
import com.tuzhi.base.util.PropertyUtil;
import org.springframework.data.domain.Sort;

import java.util.ArrayList;
import java.util.List;

public final class TSorts {
    private TSorts() {
    }

    public static Builder builder() {
        return new Builder();
    }

    public static final class Builder {
        private List<Sort.Order> orders = new ArrayList();

        public Builder() {
        }

        public Builder asc(String property) {
            return this.asc(true, property);
        }

        public Builder desc(String property) {
            return this.desc(true, property);
        }

        public Builder asc(boolean condition, String property) {
            if (condition) {
                this.orders.add(new Sort.Order(Sort.Direction.ASC, property));
            }

            return this;
        }

        public Builder desc(boolean condition, String property) {
            if (condition) {
                this.orders.add(new Sort.Order(Sort.Direction.DESC, property));
            }

            return this;
        }

        public Sort build() {
            return Sort.by(this.orders);
        }


        public <T> Builder  asc(SFunction<? super T, ?> propertyName) {
            return this.asc(true, PropertyUtil.getFieldName(propertyName));
        }

        public <T> Builder desc(SFunction<? super T, ?> propertyName) {
            return this.desc(true, PropertyUtil.getFieldName(propertyName));
        }
    }

    public static void main(String[] args) {
               /*  TSorts.builder()
                .asc(OddEntity::getId)
                .build();*/
    }
}